<?php
namespace app\index\service;

use app\index\model\CompanyModel;
use app\index\model\DeptModel;
use app\index\model\LoginFailModel;
use app\index\model\LoginListModel;
use app\index\model\LoginTelModel;
use app\index\model\LoginTokenModel;
use app\index\model\UserModel;
use think\Exception;

class LoginService
{
    public function login_by_pass($param)
    {
        $user = UserModel::get_info_by_tel($param['tel']);
        if(!$user){
            LoginTelModel::add_fail($param['tel'],$param['pass'],$param['ip']);
            throw new Exception('登录手机号码或密码错误');
        }
        if($user['pass'] != md5(md5($param['pass']))){
            LoginFailModel::add_fail($param['tel'],$param['pass'],$param['ip']);
            LoginListModel::add_row($user,$param['ip'],$param['type'],0);
            throw new Exception('登录密码错误');
        }

        if($user['status'] == 0){
            LoginListModel::add_row($user,$param['ip'],$param['type'],0);
            throw new Exception('已禁用，无法登录，请联系管理员');
        }

        $company = CompanyModel::get_info_by_pk($user['comid']);
        if(!$company || $company['status'] != 1){
            LoginListModel::add_row($user,$param['ip'],$param['type'],0);
            throw new Exception('已禁用，无法登录，请联系管理员');
        }
        if(!empty($company['enddt']) && $company['enddt'] != '0000-00-00'){
            if($company['enddt'] < date('Y-m-d')){
                throw new Exception('您的账号已到期，请联系客服人员处理');
            }
        }

        if(!$this->user_more_dept_disable($user['deptid'])){
            LoginListModel::add_row($user,$param['ip'],$param['type'],0);
            throw new Exception('已禁用，无法登录，请联系管理员');
        }

        if($user['isreg'] == 1 && $user['reg_enddt'] < date('Y-m-d H:i:s')){
            LoginListModel::add_row($user,$param['ip'],$param['type'],0);
            throw new Exception('注册体验时间已到期，请联系客服人员处理');
        }

        return $user;
    }


    public function login_by_pass222($param)
    {
        $user = UserModel::get_info_by_tel($param['tel']);
        if(!$user){
            LoginTelModel::add_fail($param['tel'],$param['pass'],$param['ip']);
            throw new Exception('登录手机号码或密码错误');
        }
        if($user['pass'] != md5(md5($param['pass']))){
            LoginFailModel::add_fail($param['tel'],$param['pass'],$param['ip']);
            LoginListModel::add_row($user,$param['ip'],$param['type'],0);
            throw new Exception('登录密码错误');
        }

        if($user['status'] == 0){
            LoginListModel::add_row($user,$param['ip'],$param['type'],0);
            throw new Exception('已禁用，无法登录，请联系管理员');
        }

        $company = CompanyModel::get_info_by_pk($user['comid']);
        if(!$company || $company['status'] != 1){
            LoginListModel::add_row($user,$param['ip'],$param['type'],0);
            throw new Exception('已禁用，无法登录，请联系管理员');
        }
        if(!empty($company['enddt']) && $company['enddt'] != '0000-00-00'){
            if($company['enddt'] < date('Y-m-d')){
                throw new Exception('您的账号已到期，请联系客服人员处理');
            }
        }

        if(!$this->user_more_dept_disable($user['deptid'])){
            LoginListModel::add_row($user,$param['ip'],$param['type'],0);
            throw new Exception('已禁用，无法登录，请联系管理员');
        }


        $tokenHour = 10;
        if(isset($param['keep']) && $param['keep'] == 1){
            $tokenHour = 7*24;
        }

        $token = create_random_str($user['id']);
        if(!LoginTokenModel::add_token($user['id'], $token, $param['ip'], $company['id'], $param['type'], 1, $tokenHour)){
            throw new Exception('登录失败');
        }
        LoginListModel::add_row($user,$param['ip'],$param['type'],1);
        $tokenStr = (new EncryptService())->dataEncrypt($token);
        return $tokenStr;
    }






    public function login_by_app($param)
    {
        $user = UserModel::get_info_by_tel($param['tel']);
        if(!$user){
            LoginTelModel::add_fail($param['tel'],$param['pass'],$param['ip']);
            throw new Exception('登录手机号码或密码错误');
        }
        if($user['pass'] != md5(md5($param['pass']))){
            LoginFailModel::add_fail($param['tel'],$param['pass'],$param['ip']);
            LoginListModel::add_row($user,$param['ip'],$param['type'],0);
            throw new Exception('登录密码错误');
        }

        if($user['status'] == 0){
            LoginListModel::add_row($user,$param['ip'],$param['type'],0);
            throw new Exception('已禁用，无法登录，请联系管理员');
        }

        $company = CompanyModel::get_info_by_pk($user['comid']);
        if(!$company || $company['status'] != 1){
            LoginListModel::add_row($user,$param['ip'],$param['type'],0);
            throw new Exception('已禁用，无法登录，请联系管理员');
        }
        if(!empty($company['enddt']) && $company['enddt'] != '0000-00-00'){
            if($company['enddt'] < date('Y-m-d')){
                throw new Exception('您的账号已到期，请联系客服人员处理');
            }
        }

        if(!$this->user_more_dept_disable($user['deptid'])){
            LoginListModel::add_row($user,$param['ip'],$param['type'],0);
            throw new Exception('已禁用，无法登录，请联系管理员');
        }

        if($user['isreg'] == 1 && $user['reg_enddt'] < date('Y-m-d H:i:s')){
            LoginListModel::add_row($user,$param['ip'],$param['type'],0);
            throw new Exception('注册体验时间已到期，请联系客服人员处理');
        }

        return $user;
    }

    public function user_more_dept_disable($deptid=0){
        if(!$deptid){
            return false;
        }
        $status = true;
        while ($deptid > 0) {
            $dept = DeptModel::where(['id'=>$deptid])->find();
            if($dept && $dept['status'] == 1 && $dept['deleted_user'] == 0){
                $deptid = $dept['pid'];
            }else{
                $status = false;
                $deptid = 0;
            }
        }
        return $status;
    }

}
